<template>
  <el-dialog
    :title="titleMap[mode]"
    v-model="visible"
    width="50%"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <el-form
      :model="form"
      :rules="rules"
      ref="dialogForm"
      :disabled="mode == 'show'"
      label-width="auto"
      label-position="right"
    >
      <el-form-item label="汇报给" style="width: 100%">
        <el-button
          type="primary"
          icon="el-icon-plus"
          circle
          @click="selectOpertaor()"
        ></el-button>
        <div class="userTag">
          <!--              v-for="tag in selectedSendUser" -->
          <el-tag
            v-for="(tag, index) in form.adminList"
            :key="tag.name"
            @close="deleteSendUser(index)"
            closable
          >
            {{ tag.name }}
          </el-tag>
        </div>
      </el-form-item>
      <el-form-item
        :label="formLabel.summaryName"
        prop="summary"
        style="width: 100%"
      >
        <el-input
          v-model="form.summary"
          clearable
          placeholder="请输入汇报事项"
          type="textarea"
          rows="3"
        ></el-input>
      </el-form-item>
      <el-form-item label="待处理问题" prop="pendingIssues" style="width: 100%">
        <el-input
          v-model="form.pendingIssues"
          clearable
          placeholder="请输入待处理问题"
          type="textarea"
          rows="3"
        ></el-input>
      </el-form-item>
      <el-form-item
        :label="formLabel.workPlanName"
        prop="workPlan"
        style="width: 100%"
      >
        <el-input
          v-model="form.workPlan"
          clearable
          placeholder="请输入工作计划"
          type="textarea"
          rows="3"
        ></el-input>
      </el-form-item>
      <el-form-item label="附件">
        <sc-upload-file
          v-model="form.otherFiles"
          :limit="10"
          :multiple="false"
          tip="最多上传10个文件,单个文件不要超过20M"
        >
          <el-button type="primary" plain icon="el-icon-upload"
            >上传附件</el-button
          >
        </sc-upload-file>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button
        v-if="mode != 'show'"
        type="primary"
        :loading="isSaveing"
        @click="submit()"
        >保 存</el-button
      >
    </template>
  </el-dialog>
  <!-- 选择经办人 -->
  <select-user ref="selectUser" @setUser="setOpertaor"></select-user>
</template>
<script>
import selectUser from "@/components/selectUser"
import { queryTypeByDic } from "@/utils/getData"
export default {
  emits: ["success", "closed"],
  components: {
    selectUser,
  },
  data () {
    return {
      mode: "add",
      titleMap: {
        add: "工作汇报新增",
      },
      visible: false,
      isSaveing: false,
      selectCustomerLoading: false,
      //表单数据
      form: {
        dailyTitle: "",
        departmentId: '',
        otherFiles: [],
        reportName: "",
        pendingIssues: "",
        reporterId: 0, //汇报人
        summary: "",
        dailyAdminList: [],
        adminList: [],
        type: 0,
        adminId: 0,
        workPlan: "",
      },
      formLabel: {
        summaryName: "",
        workPlanName: "",
      },
      //验证规则
      rules: {
        summary: [{ required: true, message: "请输入今日总结" }],
      },
    }
  },
  props: {
    userList: {
      type: Array,
      required: true,
      default: () => {
        return []
      },
    },
  },
  mounted () {
    this.getType()
  },

  methods: {
    // 获取提醒时间类型
    async getType () {
      let type = await queryTypeByDic("remindType")
      this.remindTypeList = type
    },
    //显示
    open (mode = "add", type) {
      this.mode = mode
      this.visible = true
      let userInfo = this.$TOOL.data.get("USER_INFO")
      this.form.adminId = userInfo.userId
      this.form.departmentId = userInfo.departmentId
      this.form.type = type
      if (type == 1) {
        this.formLabel = {
          summaryName: "今日工作",
          workPlanName: "明日工作",
        }
      } else if (type == 2) {
        this.formLabel = {
          summaryName: "本周工作",
          workPlanName: "下周工作",
        }
      } else if (type == 3) {
        this.formLabel = {
          summaryName: "本月工作",
          workPlanName: "下月工作",
        }
      }
      return this
    },
    // 选择经办人
    selectOpertaor () {
      this.$refs.selectUser.open(2, this.form.adminList)
    },
    setOpertaor (data) {
      this.form.adminList = data
    },
    // 删除汇报给人
    deleteSendUser (index) {
      this.form.adminList.splice(index, 1)
    },
    //表单提交方法
    submit () {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true
          let fileType = ""
          if (this.form.adminList.length === 0) {
            this.$notify.error({
              title: "错误",
              message: "请选择您要汇报的人",
            })
            return
          }
          let adminList = []
          this.form.adminList.forEach(item => {
            var object = {
              adminId: item.id
            }
            adminList.push(object)
          })
          this.form.dailyAdminList = adminList
          this.form.otherFiles.map((item) => {
            if (item.url) {
              let arr = item.url.split(".")
              let type = arr[arr.length - 1]
              // fileType  1图片 2文件
              let imageType = "jpg/png/jpeg/svg"
              if (imageType.includes(type)) {
                fileType = 1
              } else {
                fileType = 2
              }
              item.fileType = fileType
              item.fileName = item.name
            }
          })
          let data = { ...this.form }
          let res = null
          if (this.mode == "add") {
            res = await this.$API.daily.add.post(data)
          }
          this.isSaveing = false
          if (res.code == 200) {
            this.$emit("success", this.form, this.mode)
            this.visible = false
            this.$message.success("操作成功")
          }
        }
      })
    },
  },
};
</script>

<style lang="scss" scoped>
.userTag {
  margin-left: 10px;
}
</style>
